<template>
  <div class="personal">
    <div class="title">个人中心</div>
    <div class="flex">
      <div class="container">
        <div class="profile">
          <user-avatar />
        </div>
        <div class="nickname">{{ username }}</div>
        <div class="text">手机号：{{ phoneNumber }}</div>
        <div class="text">所属部门：{{ deptName }}</div>
        <div class="text">所属角色：{{ roleGroup }}</div>
        <div class="text">创建时间：2024-03-01</div>
        <el-button type="primary" @click="logout">退出登录</el-button>
      </div>
      <div class="edit">
        <h1>修改密码</h1>
        <el-form ref="form" :model="user" :rules="rules" label-width="80px">
          <el-form-item label="旧密码" prop="oldPassword">
            <el-input
              v-model="user.oldPassword"
              placeholder="请输入旧密码"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="新密码" prop="newPassword">
            <el-input
              v-model="user.newPassword"
              placeholder="请输入新密码"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item label="确认密码" prop="confirmPassword">
            <el-input
              v-model="user.confirmPassword"
              placeholder="请确认新密码"
              type="password"
              show-password
            />
          </el-form-item>
          <el-form-item>
            <el-button type="primary" size="mini" @click="submit"
              >确认修改</el-button
            >
          </el-form-item>
        </el-form>
      </div>
    </div>
  </div>
</template>

<script>
import UserAvatar from "./avatar/index.vue";
import { removeToken } from "@/utils/auth";
import { resetPassword } from "@/api/user";

export default {
  name: "Personal",
  components: { UserAvatar },
  data() {
    const equalToPassword = (rule, value, callback) => {
      if (this.user.newPassword !== value) {
        callback(new Error("两次输入的密码不一致"));
      } else {
        callback();
      }
    };
    return {
      user: {
        oldPassword: undefined,
        newPassword: undefined,
        confirmPassword: undefined,
      },
      // 表单校验
      rules: {
        oldPassword: [
          { required: true, message: "旧密码不能为空", trigger: "blur" },
        ],
        newPassword: [
          { required: true, message: "新密码不能为空", trigger: "blur" },
          {
            min: 6,
            max: 20,
            message: "长度在 6 到 20 个字符",
            trigger: "blur",
          },
          {
            pattern: /^[^<>"'|\\]+$/,
            message: "不能包含非法字符：< > \" ' \\\ |",
            trigger: "blur",
          },
        ],
        confirmPassword: [
          { required: true, message: "确认密码不能为空", trigger: "blur" },
          { required: true, validator: equalToPassword, trigger: "blur" },
        ],
      },
    };
  },
  computed: {
    username() {
      return this.$store.getters.userInfo.nickName;
    },
    phoneNumber() {
      return this.$store.getters.userInfo.phoneNumber;
    },
    deptName() {
      return this.$store.getters.userInfo.deptName;
    },
    roleGroup() {
      return this.$store.getters.userInfo.roleGroup;
    },
  },
  methods: {
    logout() {
      this.$modal
        .confirm("确定注销并退出系统吗？")
        .then(() => {
          removeToken();
          this.$router.push("/login");
        })
        .catch(() => {});
    },
    submit() {
      this.$refs["form"].validate((valid) => {
        if (valid) {
          resetPassword(this.user).then((response) => {
            this.$modal.msgSuccess("修改成功");
          });
        }
      });
    },
  },
};
</script>
<style lang="scss" scoped>
.personal {
  position: relative;
  padding: 36px 42px;
  color: #333333;

  .title {
    margin-bottom: 36px;
    height: 26px;
    line-height: 26px;
    font-size: 26px;
  }

  .flex {
    display: flex;
    .edit {
      margin-left: 40px;
    }
  }

  .container {
    position: relative;
    padding: 42px 30px 0;
    width: 770px;
    height: 540px;
    background: #ffffff;
    box-shadow: 0px 0px 24px 0px rgba(0, 0, 0, 0.06);
    border-radius: 24px;

    .profile {
      margin: 0 auto;
      width: 136px;
      height: 136px;
      border-radius: 50%;
      background-color: #333333;
      overflow: hidden;
    }

    .nickname {
      margin: 20px 0 40px;
      height: 22px;
      line-height: 22px;
      font-size: 22px;
      text-align: center;
    }

    .text {
      font-size: 18px;
      height: 66px;
      line-height: 66px;
    }

    .el-button {
      position: absolute;
      bottom: -84px;
      left: calc(50% - 144px);
      width: 288px;
      height: 48px;
      font-size: 18px;
      border-radius: 12px;
    }
  }
  .edit {
    padding: 42px 30px;
    background: #ffffff;
    box-shadow: 0px 0px 24px 0px rgba(0, 0, 0, 0.06);
    border-radius: 24px;
    h1 {
      font-size: 26px;
    }
    .el-input {
      &::v-deep .el-input__inner {
        padding-left: 20px;
        width: 400px;
        height: 40px;
        line-height: 40px;
        background: #ffffff;
        border-radius: 12px;
        border: 1px solid #dae5ff;
        font-size: 16px;
      }
    }
    .el-form-item {
      &::v-deep .el-form-item__content {
        margin: 0 auto !important;
        text-align: center;
      }
    }
    .el-button {
      width: 288px;
      height: 48px;
      font-size: 18px;
      border-radius: 12px;
    }
  }
}
</style>
